package com.hitqz.robot.dispatch.netty.util;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.collect.Maps;
import com.hitqz.robot.api.business.dto.CmdRobot2PosDto;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotApplyPathDto;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotMapPosDto;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author xupkun
 * @date 2025/4/14
 */
@Slf4j
public class CheZhouAgvUtil {

    public static ConcurrentMap<String, RobotApplyPathDto> robotApplyPathDtoMap = Maps.newConcurrentMap();

    public static ConcurrentMap<String, String> robotCurrentPos = Maps.newConcurrentMap();

    public static ConcurrentHashMap<String, Boolean> sendPathFlag = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, Boolean> sendStopFlag = new ConcurrentHashMap<>();

    private static final ConcurrentMap<String, String> currentStation = Maps.newConcurrentMap();

    private static final ConcurrentMap<String, String> lastStation = Maps.newConcurrentMap();


    /**
     * 提交路径
     * @param robotApplyPathDto
     */
    public static void submitPath(RobotApplyPathDto robotApplyPathDto) {
        if (robotApplyPathDto.getPathList().isEmpty()) {
            clearRobot(robotApplyPathDto.getClientId());
        } else {
            robotApplyPathDtoMap.put(robotApplyPathDto.getClientId(), robotApplyPathDto);
        }
    }

    public static void clearRobot(String robotSn) {
        robotApplyPathDtoMap.remove(robotSn);
        sendPathFlag.put(robotSn, false);
        sendStopFlag.put(robotSn, false);
    }


    public static void goFinalPos(String clientId, RobotMapPosDto finalPos) {
        // 更换目标
        CmdRobot2PosDto dto = new CmdRobot2PosDto();
        dto.setClientId(clientId);
        dto.setPosCode(finalPos.getCode());
        SpringUtil.getApplicationContext().publishEvent(dto);
        log.info("{} 车轴下发可以前往目标站点:{}",clientId, finalPos.getCode());
    }






}
